package com.maxlen.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maxlen.common.utils.CopyUtils;
import com.maxlen.common.utils.PageUtils;
import com.maxlen.common.utils.Query;
import com.maxlen.goods.dao.AttrDao;
import com.maxlen.goods.dao.AttrGroupDao;
import com.maxlen.goods.entity.AttrAttrgroupRelationEntity;
import com.maxlen.goods.entity.AttrEntity;
import com.maxlen.goods.entity.AttrGroupEntity;
import com.maxlen.goods.entity.vo.AttrGroupVO;
import com.maxlen.goods.entity.dto.AttrGroupDTO;
import com.maxlen.goods.entity.dto.AttrGroupWithAttrsDTO;
import com.maxlen.goods.service.AttrAttrgroupRelationService;
import com.maxlen.goods.service.AttrGroupService;
import com.maxlen.goods.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    private AttrDao attrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {
        if(categoryId.equals(0L)){
            return queryPage(params);
        }else{
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCatelogId, categoryId)
            );
            return new PageUtils(page);
        }
    }

    @Override
    public void add(AttrGroupVO attrGroupVO) {
        AttrGroupEntity attrGroupEntity = CopyUtils.copy(attrGroupVO, AttrGroupEntity.class);
        baseMapper.insert(attrGroupEntity);
    }

    @Override
    public void updateByIdDto(AttrGroupVO attrGroupVO) {
        AttrGroupEntity attrGroupEntity = CopyUtils.copy(attrGroupVO, AttrGroupEntity.class);
        baseMapper.updateById(attrGroupEntity);
    }

    @Override
    public AttrGroupDTO getDetailById(Long attrGroupId) {
        AttrGroupEntity attrGroupEntity = baseMapper.selectById(attrGroupId);
        AttrGroupDTO attrGroupDTO = CopyUtils.copy(attrGroupEntity, AttrGroupDTO.class);
        Long catelogId = attrGroupDTO.getCatelogId();

        // 根据分类 id 查找分类
        if(catelogId != null){
            attrGroupDTO.setCatelogPath(categoryService.findPathById(catelogId));
        }
        return attrGroupDTO;
    }

    @Override
    @Transactional
    public void removeDetail(List<Long> attrGroupIds) {
        baseMapper.deleteBatchIds(attrGroupIds);
        attrAttrgroupRelationService.removeDetailByAttrgroupId(attrGroupIds);
    }

    @Override
    public List<AttrGroupWithAttrsDTO> getAttrGroupWithAttrsByCategoryId(Long categoryId) {

//        List<AttrGroupEntity> attrGroupEntityList = baseMapper.selectList(new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCatelogId, categoryId));
//
//        List<AttrGroupWithAttrsVO> collect = attrGroupEntityList.stream().map(item -> {
//            List<AttrEntity> attrEntities = attrAttrgroupRelationService.getDetailById(item.getAttrGroupId());
//            AttrGroupWithAttrsVO attrGroupWithAttrsVO = CopyUtils.copy(item, AttrGroupWithAttrsVO.class);
//            attrGroupWithAttrsVO.setAttrs(attrEntities);
//            return attrGroupWithAttrsVO;
//        }).collect(Collectors.toList());
//
//        return collect;


        List<AttrGroupEntity> attrGroupEntityList = baseMapper.selectList(
                new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCatelogId, categoryId));
        List<AttrEntity> attrEntityList = attrDao.selectList(
                new LambdaQueryWrapper<AttrEntity>().eq(AttrEntity::getCatelogId, categoryId));

        if(attrGroupEntityList.isEmpty()){
            return new ArrayList<>();
        }

        List<Long> attrgroupIdList = attrGroupEntityList.stream().map(item -> {
            return item.getAttrGroupId();
        }).collect(Collectors.toList());

        List<AttrAttrgroupRelationEntity> relationEntityList = attrAttrgroupRelationService.list(
                new LambdaQueryWrapper<AttrAttrgroupRelationEntity>()
                        .in(AttrAttrgroupRelationEntity::getAttrGroupId, attrgroupIdList));

        List<AttrGroupWithAttrsDTO> attrGroupWithAttrsDTOS = attrGroupEntityList.stream().map(item -> {
            AttrGroupWithAttrsDTO withAttrsVO = CopyUtils.copy(item, AttrGroupWithAttrsDTO.class);

            List<Long> attrIdList = relationEntityList.stream().filter(relation -> {
                return relation.getAttrGroupId().equals(withAttrsVO.getAttrGroupId());
            }).map(relation -> {
                return relation.getAttrId();
            }).collect(Collectors.toList());

            List<AttrEntity> collect = attrEntityList.stream().filter(attr -> {
                return attrIdList.contains(attr.getAttrId());
            }).collect(Collectors.toList());

            withAttrsVO.setAttrs(collect);

            return withAttrsVO;
        }).collect(Collectors.toList());

        return attrGroupWithAttrsDTOS;

    }

}
